package com.yyr.callmessage.core.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.annotation.Resource;

//import javax.validation.Validation;
//import javax.validation.Validator;
//import javax.validation.ValidatorFactory;

/**
 * 配置让验证，验证一个参数错误就返回，而不是验证错误参数才返回
 * 国际化，同一个源
 */
@Configuration
public class ValidatorConfiguration {
//    @Bean
//    public Validator validator(){
//        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
//                .configure()
//                .addProperty( "hibernate.validator.fail_fast", "true" )
//                .buildValidatorFactory();
//        return validatorFactory.getValidator();
//    }

    @Resource
    private MessageSource messageSource;

    @Bean
    public org.springframework.validation.Validator initValidator(){
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
        localValidatorFactoryBean.setValidationMessageSource(messageSource);
        return localValidatorFactoryBean;
    }
}
